package com.zhou.project.modules.circle.circleGroup.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.modules.circle.circleGroup.entity.CircleGroup;
import com.zhou.project.modules.circle.circleGroup.service.CircleGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("circle/group")
@Api(tags = "[圈子组织]")
public class CircleGroupController {

    @Autowired
    private CircleGroupService circleGroupService;

    @PostMapping
    @ApiOperation("组织创建")
    @PreAuthorize("hasAuthority('group:save')")
    public void save(@RequestBody CircleGroup entity)
    {
        circleGroupService.save(entity);
    }

    @PutMapping
    @ApiOperation("组织修改")
    @PreAuthorize("hasAuthority('group:update')")
    public void update(@RequestBody CircleGroup entity)
    {
        circleGroupService.updateById(entity);
    }

    @DeleteMapping("{id}")
    @ApiOperation("组织删除")
    @PreAuthorize("hasAuthority('group:delete')")
    public void delete(@PathVariable @NotNull(message = "组织id不能为空") int id)
    {
        circleGroupService.removeById(id);
    }

    @DeleteMapping("del/{id}")
    @ApiOperation("组织物理删除")
    @PreAuthorize("hasAuthority('group:deleteTrue')")
    public void wDelete(@PathVariable @NotNull(message = "组织id不能为空") int id)
    {
        circleGroupService.delete(id);
    }

    @DeleteMapping("dbatch")
    @ApiOperation("批量删除组织")
    @PreAuthorize("hasAuthority('group:dbatch')")
    public void delete(@RequestBody List<Integer> ids)
    {
        circleGroupService.removeByIds(ids);
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询组织")
    @PreAuthorize("hasAuthority('group:query')")
    public CircleGroup query(@PathVariable  @NotNull(message = "组织id不能为空") int id)
    {
        return circleGroupService.getById(id);
    }

    @GetMapping("page")
    @ApiOperation("分类查询")
    @PreAuthorize("hasAuthority('group:page')")
    public Page<CircleGroup> query(@RequestBody @Valid CircleGroup entity)
    {
        return circleGroupService.page(entity);
    }

    @PostMapping("insert")
    @ApiOperation("创建组织并加入圈子类型")
    @PreAuthorize("hasAuthority('group:insert')")
    public void insert(@RequestBody @Valid CircleGroup entity)
    {
        circleGroupService.insert(entity);
    }

    @GetMapping("PeopleCount")
    @ApiOperation("查询该组织中总人数")
    @PreAuthorize("hasAuthority('group:people')")
    public long PeopleCount()
    {
        return circleGroupService.CountPeople();
    }

    @GetMapping("CountPosts/{id}")
    @ApiOperation("查询该组织中总贴数")
    @PreAuthorize("hasAuthority('group:posts')")
    public long PostsCount(@PathVariable  int id)
    {
        return circleGroupService.CountPosts(id);
    }



}
